#ifndef WEBSOCKET_H
#define WEBSOCKET_H

#include <QObject>
#include <QtWebSockets/QWebSocket>
#include <QJsonObject>
#include <QCryptographicHash>
#include "api.h"
class WebSocket : public QObject
{
    Q_OBJECT
public:
    explicit WebSocket(QQmlEngine *engine, QObject *parent = 0);

signals:
    void connected();
    void readyRead(const QString &action, const QVariant &data);
    void errorReported(const QString &errorString);
    void progressChanged(int value, int maximumValue);
public slots:
    void open(const QUrl &url);
    QUrl requestUrl() const;
    void onTextMessageReceived(QString message);
    void stateChanged(QAbstractSocket::SocketState state);
    bool active();

    void  post(const QString &action, const QVariantMap & data);

    void dispatch(const QVariantMap &data);
public:
    void increaseValue(int value = 1);
    void finishedProgress();
private:
    QWebSocket m_webSocket;
    QUrl m_url;

    int m_max;
    int m_value;

    QPointer<QQmlEngine> m_engine;
};

#endif // WEBSOCKET_H
